package com.cherish.web.driver.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cherish.utils.Message;
import com.cherish.web.driver.entity.Driver;
import com.cherish.web.driver.mapper.DriverMapper;
import com.cherish.web.driver.service.DriverService;
import com.cherish.web.user.entity.User;
import com.cherish.web.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:MQ
 * Date:2024/1/511:38
 */
@Service
public class DriverServiceImpl extends ServiceImpl<DriverMapper,Driver> implements DriverService {
    @Autowired
    private UserService userService;

    @Override
    public List<Driver> getDriverListByUsername(User user, JSONObject params) {
        Map<String,String> paramsMap = new HashMap<>();

        String current = params.getString("current");
        String pageSize = params.getString("pageSize");
        String queryParams = getQueryParams(params);
        paramsMap.put("actionType","query");
        paramsMap.put("username",user.getUsername());
        paramsMap.put("queryParams",queryParams);
        paramsMap.put("current",current);
        paramsMap.put("pageSize",pageSize);


        List<Driver> list = this.baseMapper.getDriverListByUsername(paramsMap);
        return list;
    }

    private String getQueryParams(JSONObject params) {
        StringBuilder paramsSql = new StringBuilder();
        // 动态查询参数
        if(params.getString("fname") != null){
            paramsSql = paramsSql.append(
                    "and FName like '%"+params.getString("fname")+"%'");
        }
        if(params.getString("fidcard") != null){
            paramsSql = paramsSql.append(
                    "and FNumber like '%"+params.getString("fidcard")+"%'");
        }
        if(params.getString("fphone") != null){
            paramsSql = paramsSql.append(
                    "and FPhone like '%"+params.getString("fphone")+"%'");
        }
        return paramsSql.toString();
    }

    @Override
    public Message saveDriverInfo(String username,Driver driver) {
        return null;
    }

    @Override
    public Message removeDriverInfo(String username,String driver) {
        return null;
    }

    @Override
    public Driver getDriverInfoById(User user,String fid) {
        Map<String,String> paramsMap = new HashMap<>();
        paramsMap.put("username",user.getUsername());
        paramsMap.put("id",fid);

        Driver driverInfo = this.baseMapper.getDriverInfoById(paramsMap);
        return driverInfo;

    }

    @Override
    public Message removeDriverById(User user,String fid) {
        Map<String,String> paramsMap = new HashMap<>();
        paramsMap.put("username",user.getUsername());
        paramsMap.put("id",fid);
        Message message = this.baseMapper.removeDriverById(paramsMap);
        return message;

    }
}
